package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("admin/setmeal")
@RestController("adminSetmealController")
@Api(tags = "套餐管理相关接口")
public class SetmealController {
    @Autowired
    private SetmealService  setmealService;

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable long id){
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO  setmealPageQueryDTO){
        PageResult pageQuery=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageQuery);
    }

    /**
     * 新增套餐
     * @return
     */
    @CacheEvict(cacheNames="userSetmealCache",allEntries=true)
    @ApiOperation("新增套餐")
    @PostMapping
    public Result<String> addSetmeal(@RequestBody SetmealDTO  setmealDTO){
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("修改套餐状态")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames="userSetmealCache",key="#id")
    public Result<String> changeStatus(@PathVariable Integer status,long id){
        log.info("修改套餐状态：{}{}",id,status);
        setmealService.changeStatus(status,id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除")
    @DeleteMapping
    @CacheEvict(cacheNames="userSetmealCache",allEntries=true)
    public Result<String> deleteByIds(Integer[] ids){
        boolean flag = setmealService.deleteByIds(ids);
        if (!flag){
            return Result.error("未选择！");
        }else{
            return Result.success();
        }
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames="userSetmealCache",allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result<String> editSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.editSetmeal(setmealDTO);
        return Result.success();
    }
}
